


/**
 * @file misc_dev.c
 * @brief 
 * @author  ()
 * @version 1.0
 * @date 2024-11-16
 * 
 * @copyright Copyright (C) 2019 Queclink Wireless Solutions (ShenZhen) Co., Ltd. All Rights Reserved.
 * 
 * @par 修改日志:
 * <table>
 * <tr><th>Date       <th>Version <th>Author  <th>Description
 * <tr><td>2024-11-16 <td>1.0     <td>     <td>内容
 * </table>
 */
#include "misc_server.h"
#include "misc_log.h"
#include "task_misc.h"
#include "global.h"
#include "flash/protocol_params/protocol_params.h"

#include "misc/misc_api.h"

#define MAGIC_AVAILD (0x55)

extern device_param_t  dev_prm;
extern device_status_t dev_sta;


/**
 * @brief 读取服务器参数
 * @param  server           My Param doc
 * @return int32_t 
 */
int32_t misc_server_param_get(server_params_t *server)
{
    assert_ptr_ret(server, -EPERM);   

    /* 设备默认参数 */
    if (dev_prm.server.magic != MAGIC_AVAILD) {
        /* 首次运行会尝试获取一次参数 */
        config_get(CONFIG_ID_MAIN_IP, &dev_prm.server);
        if (dev_prm.server.magic != MAGIC_AVAILD) {
            dev_prm.server.is_ssl = 1;
            strcpy(dev_prm.server.ipaddr, "ebike-api-test.sailvan.net");
            dev_prm.server.port = 9999;
        }
    }

    memcpy(server, &dev_prm.server, sizeof(server_params_t));

    return 0;
}

/**
 * @brief 设置服务器参数
 * @param  server           My Param doc
 * @return int32_t 
 */
int32_t misc_server_param_set(server_params_t *server)
{
    assert_ptr_ret(server, -EPERM);

    memcpy(&dev_prm.server, server, sizeof(server_params_t));
    dev_prm.server.magic = MAGIC_AVAILD;

    config_set(CONFIG_ID_MAIN_IP, &dev_prm.server);

    /* #25901 【iot】配置IP和端口后不会自动切，需要手动下发重启指令 */
    tbox_system_reboot(1000);

    return 0;
}

/**
 * @brief 设置SSL使能与否
 * @param  enable           My Param doc
 * @return int32_t 
 */
int32_t misc_server_ssl_set(bool enable)
{
    /* 设备默认参数 */
    if (dev_prm.server.magic != MAGIC_AVAILD) {
        /* 首次运行会尝试获取一次参数 */
        config_get(CONFIG_ID_MAIN_IP, &dev_prm.server);
        if (dev_prm.server.magic != MAGIC_AVAILD) {
            dev_prm.server.is_ssl = 1;
            strcpy(dev_prm.server.ipaddr, "ebike-api-test.sailvan.net");
            dev_prm.server.port = 9999;
        }
    }

    dev_prm.server.is_ssl = enable; 
    dev_prm.server.magic  = MAGIC_AVAILD;
    
    config_set(CONFIG_ID_MAIN_IP, &dev_prm.server);

    return 0;
}



const char *tls_ca_cert =
{
    "-----BEGIN CERTIFICATE-----\r\n"
    "MIIDhTCCAm2gAwIBAgIUTDO+WXfn0tzq6xXBBRNEaDR2gXswDQYJKoZIhvcNAQEL\r\n"
    "BQAwUjELMAkGA1UEBhMCY24xCzAJBgNVBAgMAmdkMQswCQYDVQQHDAJzejELMAkG\r\n"
    "A1UECgwCaHcxCzAJBgNVBAsMAmhjMQ8wDQYDVQQDDAZyb290Q0EwHhcNMjQwODA5\r\n"
    "MTE0MjUwWhcNMzQwODA3MTE0MjUwWjBSMQswCQYDVQQGEwJjbjELMAkGA1UECAwC\r\n"
    "Z2QxCzAJBgNVBAcMAnN6MQswCQYDVQQKDAJodzELMAkGA1UECwwCaGMxDzANBgNV\r\n"
    "BAMMBnJvb3RDQTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAMEgD27E\r\n"
    "7MNXOmJtqwP5noc1035uyoVCD+TkEUgA2H2O5cF9xegv7q7sy7GJL0/EnfnYAHoy\r\n"
    "hpYhqBtCCyi84PF7a3rLCUH1lX260uSN4t9GjdjGaUcZTHUgYp1xa5BnxrKt8Fzd\r\n"
    "1k60eB8nuo8YExMdEMG/n4xeprTN+08HNFDwbBKhbmBdVW5TPNPL+/+5sOyAC8f1\r\n"
    "/927QAeWyYsAEL8hs4l7KYt1wLy4IlipJe42q5tv7qesEKgRmdaZmX07sbmS+MFu\r\n"
    "QkhZFJFC2H4r9hhiFPylNU4dunb6SEZx6qL52tgBfryLqz2TT4TRfOzedwG6NbtI\r\n"
    "eOys7kS72NvaKa0CAwEAAaNTMFEwHQYDVR0OBBYEFFWlMcpojKQPF1FO4eDE6v4c\r\n"
    "Xz1oMB8GA1UdIwQYMBaAFFWlMcpojKQPF1FO4eDE6v4cXz1oMA8GA1UdEwEB/wQF\r\n"
    "MAMBAf8wDQYJKoZIhvcNAQELBQADggEBAILvaPOqwn/0aroIe4chmjv4Gc9ePzl9\r\n"
    "XJfIaWSh98GGLKgfyzVXPZo/6FvWjZAJMD9NkkFP2gwXryVudH66hOIRzZFk/t4t\r\n"
    "z4zhV3JqK5in6DqK97E7oOYYnxX+LmJRVaaBPUp1FJ1YZJkBHW5sERWFa7+tq8SA\r\n"
    "6PABq0bZu7V++Nd9TnYEYYNhj4922n8ebjx1iaJDCllQ0OyctZ5+YJYx4oMi8UIe\r\n"
    "9h1lk/eUK9OBDi32A/4+EgfM4utUGneYmXIxWy/L+jYSB6AonNbjI5lBoqay1PVw\r\n"
    "IiRgfCf1AS3Qwu/XpuMz4qipqbKOqMgAhEqRVyE0rKG+r2hPTcL4kiM=\r\n"
    "-----END CERTIFICATE-----\r\n"
};


/**
 * @brief 设置SSL使能与否
 * @param  enable           My Param doc
 * @return int32_t 
 */
int32_t misc_server_ssl_restore(void)
{
    /* 设备默认参数 */
    dev_prm.server.is_ssl = 1;
    strcpy(dev_prm.server.ipaddr, "ebike-api-test.sailvan.net");
    dev_prm.server.port = 9999;
    dev_prm.server.is_ssl = true; 
    
    config_set(CONFIG_ID_MAIN_IP, &dev_prm.server);

    misc_ssl_cacert_set(tls_ca_cert, strlen(tls_ca_cert));

    return 0;
}




/**
 * @brief 查询设备证书
 * @return const char* 
 */
const char *misc_ssl_cacert_get(void)
{
    if(dev_prm.ssl.magic != MAGIC_AVAILD) {
        aad_flash_file_rseek(FILE_CA_FILE_ID, 0);   
        if(0 >= aad_flash_file_read(FILE_CA_FILE_ID, (uint8_t *)&dev_prm.ssl, sizeof(dev_prm.ssl))) {
            return tls_ca_cert;
        }

        /* 默认证书 */
        if(dev_prm.ssl.magic != MAGIC_AVAILD) {
            return tls_ca_cert;
        }
    }

    return dev_prm.ssl.cacert;
}

/**
 * @brief 清空证书
 */
void misc_ssl_cacert_clear(void)
{
    aad_flash_file_erase(FILE_CA_FILE_ID, 0, 0);  

    memset(dev_prm.ssl.cacert, 0, sizeof(dev_prm.ssl.cacert));
    dev_prm.ssl.magic = 0;
    dev_prm.ssl.cacert_len = 0;
}

/**
 * @brief 设置/保存证书
 * @param  cert             My Param doc
 * @param  cert_len         My Param doc
 * @return int32_t 
 */
int32_t misc_ssl_cacert_set(const char *cert, uint16_t cert_len)
{
    server_ssl_params_t ssl = {0};
    assert_ptr_ret(cert, -ENOENT);

    memcpy(dev_prm.ssl.cacert, cert, cert_len);
    dev_prm.ssl.magic = MAGIC_AVAILD;
    dev_prm.ssl.cacert_len = cert_len;

    // aad_flash_file_erase(FILE_CA_FILE_ID, 0, 0);  
    aad_flash_file_wseek(FILE_CA_FILE_ID, 0);   
    if(0 != aad_flash_file_write(FILE_CA_FILE_ID, (uint8_t *)&dev_prm.ssl, sizeof(dev_prm.ssl), 0, 0)) {
        return -ENOENT;
    }

    aad_flash_file_rseek(FILE_CA_FILE_ID, 0);   
    if(0 >= aad_flash_file_read(FILE_CA_FILE_ID, (uint8_t *)&ssl, sizeof(ssl))) {
        return -ENOENT;
    }

    /* 写入后读出完成校验 */
    if (0 == strcmp(cert, ssl.cacert)) {
        log_i("update server cert file success, file_len[%d].", ssl.cacert_len);
        misc_server_ssl_set(1);
    } else {
        log_e("update server cert file fail, the cert is [%s].", cert);
        return -ENONET;
    }

    return SUCC;
}

